//
//  CZRetWeetView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZRetWeetView: UIView {

    // 记录转发微博底部约束
    var retweetViewBottomConstraint: Constraint?
    
    //定义一个ViewModel  供外界赋值
    var statusUserViewModel:CZStatusUserViewModel?{
        
        didSet{
            //转发微博正文
            contentLabel.attributedText = statusUserViewModel?.retweetAttr
            
            // 卸载约束
            retweetViewBottomConstraint?.uninstall()
            
            if statusUserViewModel?.statusModel?.retweeted_status?.pic_urls?.count > 0 {
                //有配图
                //赋值
                pictureView.pictureModel = statusUserViewModel?.statusModel?.retweeted_status?.pic_urls
                //更新约束
                self.snp_makeConstraints { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                //显示配图
                pictureView.hidden = false
                
            }else{
                //没有配图
                self.snp_makeConstraints { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
                //隐藏配图
                pictureView.hidden = true
            }
            
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        contentLabel.snp_makeConstraints { (make) in
            make.top.leading.equalTo(10)
        }
        
        //设置约束
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.leading.equalTo((SCREENW-250)/2)
        }
        
        
        self.snp_makeConstraints { (make) in
          retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
        
    }
    
    //懒加载控件
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), textSize: 15, maxWidth: SCREENW - 20)
    
    //配图
    private lazy var pictureView:CZStatusPicktureView = CZStatusPicktureView()
    
}




